﻿Ext.data.Store.prototype.currentGroup ="" ;
Ext.data.Store.prototype.currentFormat = 'scheda-uno' ;
Ext.BLANK_IMAGE_URL = "images/s.gif";
Ext.GRID_PAGE_SIZE = 17 ;
 
/*****************************************************************
						RECORDS
*****************************************************************/
 
 
var recordScheda = Ext.data.Record.create([ 
		{name: 'ID', mapping: 'ID'},
		{name: 'TOTSCHEDE', mapping: 'TOTSCHEDE'},
		{name: 'NSCHEDA', mapping: 'NSCHEDA'},
		{name: 'DESCCIMITERO', mapping: 'DESCCIMITERO' , type: 'string' },
		{name: 'DESCESTESA', mapping: 'DESCESTESA' , type: 'string' }, 
		{name: 'IDCIMITERO', mapping: 'IDCIMITERO' , type: 'string' },
		{name: 'IDPAGANTE', mapping: 'IDPAGANTE' , type: 'int'  },
		{name: 'COGNOMENOM', mapping: 'COGNOMENOM',type: 'string'}, 
		{name: 'INDIRIZZO', mapping: 'INDIRIZZO', type: 'string'},
		{name: 'COMUNE', mapping: 'COMUNE', type: 'string'},
		{name: 'CAP', mapping: 'CAP', type: 'string'},
		{name: 'TELEFONO', mapping: 'TELEFONO', type: 'string'},  
		{name: 'DEFUNTO', mapping: 'DEFUNTO', type: 'string'},
		{name: 'POSIZIONE', mapping: 'POSIZIONE', type: 'string'} 
]); 

var recordCimitero = Ext.data.Record.create([ 
		{name: 'ID', mapping: 'ID'},
		{name: 'DESCRIZION', mapping: 'DESCRIZION'},
		{name: 'DESCESTESA', mapping: 'DESCESTESA'},
		{name: 'COMUNE', mapping: 'COMUNE'}
]); 
	  	 
var params = {
	pagedGridLoadParameters : {  
		'params' : {start:0, limit: Ext.GRID_PAGE_SIZE}  ,   
		callback:function(rec,options,success){
		
			var  data = app.dsSchede.reader.jsonData.info ;
			// app.dsSchede.reader.jsonData.info
		 
			var tpl = new Ext.XTemplate(
			    '<ul>',
				'<li>Cimitero : {cimitero} </li>',
				'<li>N° Schede Trovate : {cimitero} </li>',
				'<li>Ultimo N° Scheda : {cimitero} </li>',
				'</ul>'	
			);
 
			tpl.overwrite(  Ext.getCmp('info-panel').body, {cimitero : data.cimitero } );
		} 
	}  
}	 

var app = {
	dsSchede : null 
}
Ext.onReady(function(){
	Ext.QuickTips.init();
		 
	
	var title = "Gestione Anagrafica" ;
	document.title = title ; 
 	 
	/*****************************************************************
							STORE
	*****************************************************************/
	var dsSchede =  new Ext.data.Store({
		remoteSort:true, 
		baseParams:{action:'list'},
		record: recordScheda  , 
		proxy : new Ext.data.HttpProxy({  url: "schede/data.php" }),  
		reader : new Ext.data.JsonReader(
					{ root: 'rows',  totalProperty: 'results',  id: 'rowId' }, 
					recordScheda 
		)
		
	});
	
	app.dsSchede = dsSchede ;	
	//dsSchede.load(  params.pagedGridLoadParameters  ); 
	   
	var dsComboCimiteri =  new Ext.data.Store({
		remoteSort:false, 
		baseParams:{action:'combo'},
		record: recordCimitero  , 
		proxy : new Ext.data.HttpProxy({  url: "cimiteri/data.php" }),  
		reader : new Ext.data.JsonReader(
					{ root: 'rows',  totalProperty: 'results',  id: 'rowId' }, 
					recordCimitero 
		)
		
	}); 
	 
	var comboCimiteriFormSchede = new Ext.form.ComboBox		({ 	
		fieldLabel:"Cimitero" ,  
		loadingText: 'Caricamento...',  
		pageSize:10, 
		store: dsComboCimiteri,  
		displayField:'DESCESTESA',  
		valueField:'ID',   
		hiddenId:Ext.id(),  
		hiddenName:'IDCIMITERO',  
		typeAhead: true , 
		triggerAction: 'all',
		lazyRender:false,
		emptyText: '',
		mode : 'remote' , 			
		minChars :1 ,
		listWidth :255 ,
		selectOnFocus:true , 
		queryParam : 'vc__ID' ,
		width:220
	});
	 
	Ext.Ajax.request({url : 'schede/window.def' , 
		success: function ( resp, opt ) { 
			eval(  "new Ext.Window (" + resp.responseText  + ").render(document.body)"  ) ;  
		}, 
		failure: function ( resp, opt ) { alert("Errore : schede/window.def  " ) } 
	}); 
	
	var viewport = new Ext.Viewport({ 
		layout:'border',  
		items: [   
			{  
				frame:false,
				region:'north', 
				margins:'0 0 0 0', 
				border: false,
				height:53,
				el: 'application-header',
				items : [ 
					{frame:false, border:false ,  html:'<h1>Elettronica C-Lux</h1>', margins:'0 0 0 0' , xtype:'panel'} , 
					
					{
						frame:false, 
						border:false , 
						tbar:  [
									{   text:'Stampe', tooltip: {title:'Stampe',text:'Elenco Stampe Disponibili'},
										iconCls: 'icon-stampe' ,
										border: false,
										menu:{
											id:'menu-stampe', 
											width:200,
											items: [
												{ text:'Stampa Bollettini',  scope:this , iconCls: 'icon-stampe' },
												{ text:'Stampa Conti Correnti',   scope:this  , iconCls: 'icon-stampe' },
												'-',
												{ text:'Stampa Elenco',   scope:this  , iconCls: 'icon-stampe' }
											]
										}
									},{ 
										text:'Aggiungi', tooltip: {title:'Aggiungi',text:'Aggiunta Paganti e schede'},
										iconCls: 'icon-add-card' ,
										menu:{ 	id:'menu-aggiungi', 
												width:200,
												items: [
													{ text:'Aggiungi Pagante',   scope:this , iconCls:'icon-person'  },
													'-',
													{ text:'Aggiungi Scheda',   scope:this  , iconCls:'icon-card'}
												]
										}
									} 
							],	
						 
						margins:'0 0 0 0' ,
						xtype:'panel'
					}
					
				] 
			} ,
			
			{
				xtype:'panel',
				id:'application-left-panel',
				region:'west',
				margins:'0 2 0 0',
				split:true,
				collapsible: true,
				collapseMode: 'mini',
				width:230,
				minWidth: 230,
				border: true,
				baseCls:'x-plain', 
				items : [   
					{
						xtype:'form',
						iconCls:'icon-search',	
						title: 'Ricerca',
						collapsible:true,
						id:'schede-search-panel',
						formId:'schede-search-form',
						labelWidth : 65,
						frame:true,
						defaultType: 'textfield', 
						items :[  
		                    { fieldLabel: 'Cimitero', name: 'vc__IDCIMITERO' , width:55},
							{ xtype:'numberfield' , fieldLabel:"N°Scheda" , 	name : 'in__NSCHEDA' , 	width:55 },
							{ fieldLabel:"Pagante" , 	name : 'vc__COGNOMENOM' ,		width:133 }, 
							{ fieldLabel:"Defunto" , 	name : 'vc__DEFUNTO' ,		width:133 }, 
							{ fieldLabel:"Posizione" , 	name : 'vc__POSIZIONE' ,		width:133 } 
						],
						
						buttons: [
							{ 	
								text: 'Nuova Ricerca', handler : function(){
									var thisForm = Ext.getCmp("schede-search-panel").form ;
									var fieldIdCimitero = thisForm.findField("vc__IDCIMITERO");
									var fieldIdCimiteroValue = fieldIdCimitero.getValue();
									
									thisForm.reset();
									 
									fieldIdCimitero.setValue( fieldIdCimiteroValue );
								}
							},
							{ 	id:'button-schede-search-trova',
								text: 'Trova' , handler : function(){
									var gridPanel = Ext.getCmp("schede-grid") ;
									var thisStore =  gridPanel.store ; 
									var thisForm = Ext.getCmp("schede-search-panel").form ; 
									var values = thisForm.getValues(false) ;
									 
									for(val in values) thisStore.baseParams[val] = values[val];
									thisStore.baseParams["action"] ='list' ; 
									
									thisStore.load(   params.pagedGridLoadParameters   );
									 
								} 
							}// Trova
						]
					},
					
					{
						title:"Informazioni",
						frame:true, 
						id:'info-panel', 
						url : 'cimiteri/data.php?action=info' ,
						xtype:'panel',
						iconCls:'icon-information',
						html : 	'<ul>' +
								'<li><b>Informazioni relative a: </b></li>' +
								'<li>Cimiteri</li>'  +
								'<li>Risultati ricerca</li>'   +
								'<li>Situazione pagante</li>'   +
								'</ul>'  
					}
					
				] //items
			},
			{
				xtype:'grid',
				bbar : new Ext.PagingToolbar({ 
					pageSize: Ext.GRID_PAGE_SIZE, 
					displayInfo: true,
					displayMsg: 'Schede dalla <b>{0}</b> alla <b>{1}</b> su un totale di <b>{2}</b>',
					store : dsSchede, 
					emptyMsg: "Niente da visualizzare" 
				}) ,
				
				id:'schede-grid',
				loadMask: {msg:'Caricamento in corso ...'},
				ds: dsSchede,
				title : 'Elenco Schede' ,
				listeners: {
				    rowdblclick : function(grid , rowIndex, event){
						var windSchede  = Ext.getCmp("window-schede");
						var formSchede = Ext.getCmp("schede-panel").form;
						var record = grid.store.getAt(rowIndex) ;  
						  
						formSchede.loadRecord(   record  );
						comboCimiteriFormSchede.setValue(record.data.DESCESTESA);
						 
						windSchede.show();
					}
				},
				cm: new Ext.grid.ColumnModel([  
							{ 	header: 'Cimitero' , 
								dataIndex: 'IDCIMITERO', 
								renderer:function (value, p, record){
											return 	record.data.DESCESTESA   ;
													
								}, 
								width: 150 , 
								sortable:true  }, 
							{ 	header: 'N°Scheda' , dataIndex: 'NSCHEDA', width: 60, sortable:true},  
							{ 	header: 'Pagante', 
								dataIndex: 'COGNOMENOM',
								renderer:function (value, p, record){
											return 	"<b>" + record.data.COGNOMENOM  	+ "</b><br>" + 
													record.data.INDIRIZZO + " <br> " +
													"<span style='color:red;font-weight:bold'>" + record.data.CAP + " " +record.data.COMUNE + "</span><br>" + 
													record.data.TELEFONO				+ "<br>"   ;
													
								}, 
								width: 250 , sortable:true
							},
							{ 	header: 'Defunto', 
								dataIndex: 'DEFUNTO', 
								renderer:function (value, p, record){
									return 	"<span style='color:darkblue;font-weight:bold'>" + record.data.DEFUNTO +"</span><br>" +record.data.POSIZIONE 	+ 
											"<br>" +  record.data.IDCIMITERO + " - " + record.data.DESCCIMITERO
								},
								width: 250, 
								sortable:true
							}  
					]),
					
				viewConfig: {
					forceFit:false, 
					getRowClass : function(record, index){
			 
						if(dsSchede.currentGroup != record.data.IDPAGANTE ) {
							dsSchede.currentGroup = record.data.IDPAGANTE ;
							if(dsSchede.currentFormat=='scheda-uno') dsSchede.currentFormat='scheda-due' ;
							else dsSchede.currentFormat='scheda-uno' ;				
						}  
						
						return dsSchede.currentFormat ;  
					} 
				}, 
				collapsible: false , 
				animCollapse: false ,
				region: 'center' ,
				margins:'2 4 4 2',
				iconCls:'icon-list',	
				
				autoScroll  : true 
			}
			
			
		]} 
	
	);
	
	//mapping Trova button
	new Ext.KeyMap("schede-search-panel", { key: 13,  fn: Ext.getCmp("button-schede-search-trova").handler , scope: Ext.getCmp("schede-search-panel") });
	 
});